/*
	Copyright (c) 2010, Mathias Myrland, Myrland Systemutvikling (mathias@myrsys.no)
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:
	
	    * Redistributions of source code must retain the above copyright
	      notice, this list of conditions and the following disclaimer.
	      
	    * Redistributions in binary form must reproduce the above copyright
	      notice, this list of conditions and the following disclaimer in the
	      documentation and/or other materials provided with the distribution.
	      
	    * Neither the name of Myrland Systemutvikling nor the
	      names of its contributors may be used to endorse or promote products
	      derived from this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
	ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
	WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
	DISCLAIMED. IN NO EVENT SHALL Myrland Systemutvikling BE LIABLE FOR ANY
	DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
	ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CGF_ATTRIBUTE_FACTORY_HPP
#define CGF_ATTRIBUTE_FACTORY_HPP

#include <cgf/entity.hpp>
#include <cgf/object.hpp>
#include <cgf/error.hpp>

#include <boost/unordered_map.hpp>
#include <boost/function.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>

namespace CGF
{

	
	template<class t_value, class t_owner>
	boost::shared_ptr<Attribute> attBasetypeCreator ( std::string value, boost::shared_ptr<t_owner> owner )
	{
		return boost::shared_ptr<Attribute> ( new Attribute_T<t_value, t_owner> ( boost::lexical_cast<t_value> ( value ), owner ) );
	}
	
	
			
	template<class t_owner>
	class AttributeFactory
	{
		
		/**	Attribute creation function map	*/
		
		boost::unordered_map<std::string, boost::function<boost::shared_ptr<Attribute> ( std::string, boost::shared_ptr<t_owner> ) > > m_attributeCreators;
		
		public:
			
			/**
			*	Create an attribute based on type with a provided name and value
			*	@param	type
			*	@param	value
			*	@param	owner	The t_owner which will own this attribute
			*	@tparam	owner	The type of the owner
			*	@return		The newly created attribute	*/
			
			boost::shared_ptr<Attribute> AttributeFactory::createAttribute ( std::string type , std::string value, boost::shared_ptr<t_owner> owner )
			{
				//	Precaution
				if ( !m_attributeCreators.count ( type ) )
					throw Error ( "Attribute creator for type " + type + " is not registered. " );
				
				//	Create the attribute
				return m_attributeCreators[type] ( value, owner );
			}
						

			/**
			*	Register the creator function object with a typename
			*	@param	name
			*	@param	function		A function object which creates an attribute of the specified type. Must be on the form shared_ptr<Attribute> (string, string, string)	*/
			void AttributeFactory::registerCreator ( std::string type, boost::function<Object<Attribute>::ptr ( std::string, boost::shared_ptr<t_owner> ) > func )
			{
				m_attributeCreators[type] = func;
			}

	
			AttributeFactory::AttributeFactory ()
			{
				m_attributeCreators["int"] = boost::bind ( &attBasetypeCreator<int32_t, t_owner>, _1, _2 );
				m_attributeCreators["int64"] = boost::bind ( &attBasetypeCreator<int64_t, t_owner>, _1, _2 );
				m_attributeCreators["uint"] = boost::bind ( &attBasetypeCreator<uint32_t, t_owner>, _1, _2 );
				m_attributeCreators["uint64"] = boost::bind ( &attBasetypeCreator<uint64_t, t_owner>, _1, _2 );
				m_attributeCreators["float"] = boost::bind ( &attBasetypeCreator<float, t_owner>, _1, _2 );
				m_attributeCreators["double"] = boost::bind ( &attBasetypeCreator<double, t_owner>, _1, _2 );
				m_attributeCreators["string"] = boost::bind ( &attBasetypeCreator<std::string, t_owner>, _1, _2 );
			}
		
		private:
			
		
	};
	
}//	namespace CGF
#endif
